package com.gzx.plugin.tjzy.modular.sse.controller;

import com.gzx.plugin.tjzy.modular.sse.service.GzxSseService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import vip.xiaonuo.common.pojo.CommonResult;

/**
 * @author zjt
 * @description
 * @date 2025/4/29 15:56
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tjzy/sse")
public class GzxSseController {

    private final GzxSseService sseService;

    @GetMapping("init/{clientId}")
    public SseEmitter initSse(@PathVariable String clientId) {
        return sseService.initSse(clientId);
    }

    /**
     * 发送消息
     */
    @GetMapping("/sendMsg")
    public CommonResult<Void> sendMsg() {
        sseService.sendMessage();
        return CommonResult.ok();
    }

    /**
     * 给所有人发通知
     */
    @GetMapping("/sendAll/{type}")
    public CommonResult<Void> sendAll(@PathVariable("type") String type, @RequestParam("msg") String msg) {
        sseService.sendAll(type, msg);
        return CommonResult.ok();
    }
}
